/*
 *  Copyright 2012 Anton Van Zyl. http://code.google.com/p/java-swiss-knife/
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.knife.Math;

import java.math.BigDecimal;

import com.knife.Math.exception.InvalidDataException;

/**
 * This is a extension on the Math class in java;<br/>
 * Catering for all the extra math solutions that I needed throughout.
 * 
 * <br/>
 * <br/>
 * Please visit <a
 * href="http://code.google.com/p/java-swiss-knife/">Java-Swiss-Knife</a> and
 * comment, rate, contribute or raise a issue/enhancement for my library. <br/>
 * 
 * @author Anton Van Zyl
 * 
 */
public final class Math {

	/**
	 * This will take a <code>long</code> and invert it.<br/>
	 * If the number is positive, a negative will be the expected result. Note
	 * that a value of <code>0</code> cannot be converted and a
	 * <code>InvalidDataException</code> will be thrown.
	 * 
	 * @param input
	 *            - Value to be inverted
	 * @return inverted value
	 * @throws InvalidDataException
	 */
	public static long invertNumber(long input) throws InvalidDataException {
		long result = 0;
		if (input == 0) {
			throw new InvalidDataException("A value of 0 cannot be inverted");
		}
		if (input > 0) {
			String temp = "-" + String.valueOf(input);
			result = Long.parseLong(temp);
		} else {
			String temp = String.valueOf(input);
			int minusLocation = temp.indexOf('-');
			temp = temp.substring(minusLocation + 1);
			result = Long.parseLong(temp);
		}
		return result;
	}

	/**
	 * In the Fibonacci sequence of numbers, each number is the sum of the
	 * previous two numbers.</br> This sequence begins: 1, 1, 2, 3, 5, 8, 13,
	 * 21, 34, 55, 89,... <br/>
	 * 
	 * @param rounds
	 *            - The number of rounds the sum will run in the loop
	 * @return fibonacci calculation
	 */
	public static BigDecimal fibonacci(int rounds) {
		BigDecimal numberOne = new BigDecimal(0);
		BigDecimal numberTwo = new BigDecimal(1);
		BigDecimal total = new BigDecimal(0);
		BigDecimal result = numberTwo;
		for (int x = 0; x < rounds; x++) {
			total = total.add(result);
			result = numberOne.add(numberTwo);
			numberOne = numberTwo;
			numberTwo = result;
		}

		return total;
	}

	/**
	 * This will calculate the average of the sum of rounds.<br/>
	 * 
	 * In the Fibonacci sequence of numbers, each number is the sum of the
	 * previous two numbers.</br> This sequence begins: 1, 1, 2, 3, 5, 8, 13,
	 * 21, 34, 55, 89,... <br/>
	 * <br/>
	 * <br/>
	 * Please visit I was interviewed by Vaadin and was asked to produce this
	 * solution for them in 2012.
	 * 
	 * @param rounds
	 *            - The number of rounds the sum will run in the loop
	 * @return - fubunacci calculation average
	 */
	public static BigDecimal fibonacciAverage(int rounds) {

		BigDecimal numberOne = new BigDecimal(0);
		BigDecimal numberTwo = new BigDecimal(1);
		BigDecimal total = new BigDecimal(0);
		BigDecimal result = numberTwo;
		for (int x = 0; x < rounds; x++) {
			total = total.add(result);
			result = numberOne.add(numberTwo);
			numberOne = numberTwo;
			numberTwo = result;
		}

		return total.divide(new BigDecimal(rounds));
	}

}
